package acm.pta3;

import java.util.*;

public class T6 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        int n = sc.nextInt();
        int rank=1;
        PriorityQueue<Node>[] queue=new PriorityQueue[2];
        queue[0]=new PriorityQueue<Node>(Comparator.comparingInt(o -> o.rank));
        queue[1]=new PriorityQueue<Node>((o1, o2) -> o2.rank-o1.rank);
        while (n-- > 0) {
            queue[sc.nextInt()].add(new Node(rank++,sc.next()));
        }
        PriorityQueue<Node[]>resQueue=new PriorityQueue<>(Comparator.comparingInt(o -> o[0].rank));
        while (!queue[0].isEmpty()) {
            Node girl = queue[0].poll();
            Node boy = queue[1].poll();
            if (girl.rank < boy.rank) {
                resQueue.add(new Node[]{girl,boy});
            }else {
                resQueue.add(new Node[]{boy,girl});
            }
        }
        while (!resQueue.isEmpty()) {
            Node[] nodes = resQueue.poll();
            System.out.println(nodes[0]+" "+nodes[1]);
        }
    }
//    public static StringBuilder fun(int[] sex,String[] names){
//        int rank=1;
//        PriorityQueue<Node>[] queue=new PriorityQueue[2];
//        queue[0]=new PriorityQueue<Node>(MyComparator.comparingInt(o -> o.rank));
//        queue[1]=new PriorityQueue<Node>((o1, o2) -> o2.rank-o1.rank);
//        for (int i = 0; i < names.length; i++) {
//            queue[sex[i]].add(new Node(rank++,names[i]));
//        }
//        PriorityQueue<Node[]>resQueue=new PriorityQueue<>(MyComparator.comparingInt(o -> o[0].rank));
//        while (!queue[0].isEmpty()) {
//            Node girl = queue[0].poll();
//            Node boy = queue[1].poll();
//            if (girl.rank < boy.rank) {
//                resQueue.add(new Node[]{girl,boy});
//            }else {
//                resQueue.add(new Node[]{boy,girl});
//            }
//        }
//        StringBuilder sb=new StringBuilder();
//        while (!resQueue.isEmpty()) {
//            Node[] nodes = resQueue.poll();
//            sb.append(nodes[0]).append(" ").append(nodes[1]).append("\n");
//        }
//        return sb;
//    }

    static class Node{
        int rank;
        String name;

        public Node(int rank, String name) {
            this.rank = rank;
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }
//    static {
//        compare();
////        int[] sex={0,1,0,0,1,1};
////        String[] names={"r0","r1","r2","r3","r4","r5"};
////        System.out.println(fun(sex, names));
//    }
//    public static void compare(){
//        int count=1000;
//        Random r=new Random();
//        while (count-- > 0) {
//            int n=(r.nextInt(10)+1)*2;
//            int girl=n/2;
//            int boy=n/2;
//            int[] sex=new int[n];
//            String[] name=new String[n];
//            for (int i = 0; i < sex.length; i++) {
//                if (r.nextBoolean()&&girl-->0){
//                    sex[i]=0;
//                }else if (boy-->0){
//                    sex[i]=1;
//                }
//                name[i]="rank"+i;
//            }
//            StringBuilder fun = fun(sex, name);
//            StringBuilder answer = Test.answer(sex, name);
//            if (!fun.equals(answer)){
//                System.out.println("---------------------------------------");
//                System.out.println("预期："+answer);
//                System.out.println("实际："+fun);
//                for (int i = 0; i < sex.length; i++) {
//                    System.out.print(name[i]+","+sex[i]+"\t");
//                }
//                System.out.println();
//            }
//        }
//    }
}
